package com.athena.crm.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.athena.crm.domain.bo.TCompanyUnlockBo;
import com.athena.crm.service.ICrmClueService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.athena.common.annotation.RepeatSubmit;
import com.athena.common.annotation.Log;
import com.athena.common.core.controller.BaseController;
import com.athena.common.core.domain.PageQuery;
import com.athena.common.core.domain.R;
import com.athena.common.core.validate.AddGroup;
import com.athena.common.core.validate.EditGroup;
import com.athena.common.core.validate.QueryGroup;
import com.athena.common.enums.BusinessType;
import com.athena.common.utils.poi.ExcelUtil;
import com.athena.crm.domain.vo.TCompanyVo;
import com.athena.crm.domain.bo.TCompanyBo;
import com.athena.crm.service.ITCompanyService;
import com.athena.common.core.page.TableDataInfo;

/**
 * 企业信息
 *
 * @author ruoyi
 * @date 2023-06-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/crm/company")
public class TCompanyController extends BaseController {

    private final ITCompanyService iTCompanyService;

    /**
     * 查询企业信息列表
     */
    @SaCheckPermission("crm:company:list")
    @GetMapping("/list")
    public TableDataInfo<TCompanyVo> list(TCompanyBo bo, PageQuery pageQuery) {
        return iTCompanyService.queryPageList(bo, pageQuery);
    }

    /**
     * 客户公海信息转为线索数据
     */
    @SaCheckPermission("crm:company:chageclue")
    @GetMapping("/chageclue")
    public void chageclue(TCompanyBo bo) {
        iTCompanyService.chageclue(bo);
    }

    /**
     * 导出企业信息列表
     */
    @SaCheckPermission("crm:company:export")
    @Log(title = "企业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TCompanyBo bo, HttpServletResponse response) {
        List<TCompanyVo> list = iTCompanyService.queryList(bo);
        ExcelUtil.exportExcel(list, "企业信息", TCompanyVo.class, response);
    }

    /**
     * 获取企业信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("crm:company:query")
    @GetMapping("/{id}")
    public R<TCompanyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iTCompanyService.queryById(id));
    }

    /**
     * 新增企业信息
     */
    @SaCheckPermission("crm:company:add")
    @Log(title = "企业信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TCompanyBo bo) {
        return toAjax(iTCompanyService.insertByBo(bo));
    }

    /**
     * 修改企业信息
     */
    @SaCheckPermission("crm:company:edit")
    @Log(title = "企业信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TCompanyBo bo) {
        return toAjax(iTCompanyService.updateByBo(bo));
    }

    /**
     * 删除企业信息
     *
     * @param esIds 主键串
     */
    @SaCheckPermission("crm:company:remove")
    @Log(title = "企业信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{esIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] esIds) {
        return toAjax(iTCompanyService.deleteWithValidByIds(Arrays.asList(esIds), true));
    }

    /**
     * 删除企业信息
     *
     * @param bo 主键串
     */
    @SaCheckPermission("crm:company:unlock")
    @Log(title = "企业信息", businessType = BusinessType.UNLOCK)
    @PostMapping("/unlock")
    public R<Void> unlock(@Validated(AddGroup.class) @RequestBody TCompanyUnlockBo bo) {
        return toAjax(iTCompanyService.unlockByBo(bo, getUserId()));
    }
}
